<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019\6\16 0016
 * Time: 16:12
 */
namespace BigVideo\IMooc\UserStorage;

use BigVideo\IMooc\Factory;

/**
 * 迭代器模式的使用
 * Class findAllUser
 * @package BigVideo\IMooc\UserStorage
 */
class findAllUser implements \Iterator
{
    private $ids;
    private $index;
    private $data = [];
    public function __construct()
    {
        $db = Factory::createDataBase();
        $this->ids = $db->findAllUser("id");
    }

    /**
     * 当前元素
     */
    public function current()
    {
        $id = $this->ids[$this->index]['id'];
        $db = Factory::createDataBase();
        return $db->findUser($id);
    }

    /**
     * 下一个元素
     */
    public function next()
    {
        $this->index += 1;
    }

    /**
     * 重置迭代器（从1开始）
     */
    public function rewind()
    {
        $this->index = 0;
    }

    /**
     * 验证是否还有下一个元素
     */
    public function valid()
    {
        return count($this->ids) > $this->index;
    }

    /**
     * 当前元素位置
     */
    public function key()
    {
        return $this->index;
    }
}